from pydantic_settings import BaseSettings 
from typing import List

class Settings(BaseSettings):
    PROJECT_NAME: str = "DOI Downloader"
    API_V1_STR: str = "/api/v1"
    BACKEND_SERVER: str = ""
    
    # Redis配置 - 删除默认值，完全依赖.env文件
    REDIS_HOST: str = "redis"  # 使用Docker服务名作为默认值
    REDIS_PORT: int = 6379
    REDIS_PASSWORD: str = "" 
    # Sci-Hub 域名配置
    SCIHUB_DOMAINS: List[str] = [
        "https://sci-hub.ren/",
        "https://sci-hub.ee/",
        "https://www.sci-hub.in/",
        "https://sci-hub.mk/",
        "https://pismin.com/",
        "https://sci-hub.st/",
        "https://sci-hub.ru/",
        "https://sci-hub.se/",
    ]
    MAX_DOWNLOAD:int=3
    BACKEND_PORT:int=8000
    
    # SQLite配置
    SQLITE_DB_FILE: str = "doi_downloader.db"
    
    # PostgreSQL配置
    DB_TYPE: str = "sqlite"  # 默认使用sqlite，可选值：sqlite, postgresql
    POSTGRES_SERVER: str = "localhost"
    POSTGRES_USER: str = "postgres"
    POSTGRES_PASSWORD: str = "postgres"
    POSTGRES_DB: str = "doi_downloader"
    POSTGRES_PORT: int = 5432
    
    @property
    def DATABASE_URL(self) -> str:
        """根据配置返回数据库连接URL"""
        if self.DB_TYPE.lower() == "postgresql":
            return f"postgresql://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}@{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}/{self.POSTGRES_DB}"
        else:
            return f"sqlite:///{self.SQLITE_DB_FILE}"
    
    # Celery配置
    @property
    def CELERY_BROKER_URL(self) -> str:
        encoded_password = self.REDIS_PASSWORD
        return f"redis://:{encoded_password}@{self.REDIS_HOST}:{self.REDIS_PORT}/0"
    
    @property
    def CELERY_RESULT_BACKEND(self) -> str:
        encoded_password = self.REDIS_PASSWORD
        return f"redis://:{encoded_password}@{self.REDIS_HOST}:{self.REDIS_PORT}/0"
    
    # 文件上传配置
    UPLOAD_DIR: str = "uploads"
    DOWNLOAD_DIR: str = "downloads"
    
    class Config:
        case_sensitive = True
        env_file = ".env"  # 从.env文件加载配置
        extra = "ignore"  # 忽略.env文件中未定义的字段，避免报错

settings = Settings()